package org.csu.logistics.Controller;

import org.csu.logistics.Service.OrderOVService;
import org.csu.logistics.Service.OrderService;
import org.csu.logistics.Service.TransCenterService;
import org.csu.logistics.Service.WarehouseService;
import org.csu.logistics.common.CommonResponse;
import org.csu.logistics.entity.TransCenter;
import org.csu.logistics.entity.Warehouse;
import org.csu.logistics.vo.OrderOV;
import org.csu.logistics.vo.WarehouseOV;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin(origins = "http://47.100.72.98:80")
@RequestMapping("/OrderController/")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderOVService orderOVService;

    @Autowired
    private WarehouseService warehouseService;

    @Autowired
    private TransCenterService transCenterService;

    //接口4 填写申请订单相关信息
    @PostMapping("/confirm")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<OrderOV> Insert_orderOV(@Validated @RequestBody OrderOV orderOV)
    {
        OrderOV orderOV1 = orderOVService.insert_OrderOV(orderOV);

        return CommonResponse.createForSuccess(orderOV1);
    }

    //接口10 个人所有状态的订单返回
    @PostMapping("/checkOrder")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<List<OrderOV>> Select_orderOV(@RequestBody Map<String,String> data){
        return orderOVService.select_OrderOVList(Integer.parseInt(data.get("userId")));
    }

    //接口6 查询所有个人运输中的订单
    @PostMapping("/checkOrderByUserId")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<List<OrderOV>> Select_orderOVByUserId(@RequestBody Map<String,String> data){
        return orderOVService.select_OrderOVListByUserId(Integer.parseInt(data.get("userId")));
    }

    //接口12 17 根据状态返回订单
    @PostMapping("/checkOrderByOrderStatus")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<List<OrderOV>> Select_orderOVByOrderStatus(@RequestBody Map<String,String> data){
        return orderOVService.select_OrderOVListByOrderStatus(data.get("orderStatus"));
    }

    @PostMapping("/checkTransCenter")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<List<WarehouseOV>> Select_warehouseOVByOrderNum(@RequestBody Map<String,String> data){
        List<Warehouse> warehouseList = warehouseService.select_wareHouseByOrderNum(Integer.parseInt(data.get("orderNum")));
        if(warehouseList==null){
            return CommonResponse.createForError("没有该订单仓库信息");
        }
        List<WarehouseOV> warehouseOVList = new ArrayList<>();
        for(int i=0;i<warehouseList.size();i++){
            Warehouse warehouse = warehouseList.get(i);
            TransCenter transCenter = transCenterService.select_transCenterByWareHouseId(warehouse.getWarehouseId());
            WarehouseOV warehouseOV = new WarehouseOV();
            warehouseOV.setWarehouseId(warehouse.getWarehouseId());
            warehouseOV.setOrderNum(warehouse.getOrderNum());
            warehouseOV.setStatus(warehouse.getStatus());
            warehouseOV.setLocale(transCenter.getLocale());
            warehouseOVList.add(warehouseOV);
        }
        return CommonResponse.createForSuccess(warehouseOVList);
    }

    //接口8 更新售后
    @PostMapping("/updateAfterSale")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<String> Update_afterSale(@RequestBody Map<String,String> data){
        OrderOV orderOV = orderOVService.select_OrderOVByOrderNum(Integer.parseInt(data.get("orderNum")));
        if(orderOV==null){
            return CommonResponse.createForError("订单错误");
        }
        orderOV.setAfterSale(data.get("afterSale"));
        orderOV.setOrderStatus("待售后");
        orderOVService.update_OrderOVByOrderNum(orderOV);
        return CommonResponse.createForSuccess("更新完成");
    }

    //接口9 更新完成
    @PostMapping("/updateEvaluate")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<String> Update_evaluate(@RequestBody Map<String,String> data){
        OrderOV orderOV = orderOVService.select_OrderOVByOrderNum(Integer.parseInt(data.get("orderNum")));
        if(orderOV==null){
            return CommonResponse.createForError("订单错误");
        }
        orderOV.setEvaluate(Integer.parseInt(data.get("evaluate")));
        orderOV.setOrderStatus("已完成");
        orderOVService.update_OrderOVByOrderNum(orderOV);
        return CommonResponse.createForSuccess("更新完成");
    }

    //接口13 状态修改
    @PostMapping("/updateTrans")
    @CrossOrigin(origins = "http://47.100.72.98:80")
    @ResponseBody
    public CommonResponse<String> Update_Trans(@RequestBody Map<String,String> data){
        OrderOV orderOV = orderOVService.select_OrderOVByOrderNum(Integer.parseInt(data.get("orderNum")));
        if(orderOV==null){
            return CommonResponse.createForError("订单错误");
        }
        orderOV.setOrderStatus(data.get("orderStatus"));
        if(data.get("orderStatus").equals("运输中")){
            String origin = orderOV.getOrigin();
            TransCenter transCenter = transCenterService.select_transCenterByLocale(origin);
            int id = transCenter.getWarehouseId();

            String destination = orderOV.getDestination();
            TransCenter transCenter1 = transCenterService.select_transCenterByLocale(destination);
            int id1 = transCenter1.getWarehouseId();

            String way = "";
            if(id1==1 && id!=1) {
                way = transCenter.getFirst();
            }
            if(id1==2 && id!=2) {
                way = transCenter.getSecond();
            }
            if(id1==3 && id!=3) {
                way = transCenter.getThird();
            }
            if(id1==4 && id!=4) {
                way = transCenter.getFourth();
            }
            if(id1==5 && id!=5) {
                way = transCenter.getFifth();
            }
            if(id1==6 && id!=6) {
                way = transCenter.getSixth();
            }
            if(id1==7 && id!=7) {
                way = transCenter.getSeventh();
            }
            if(id1==8 && id!=8) {
                way = transCenter.getEighth();
            }
            if(!way.equals("")){
                String[] Tokens = way.split("[|]");
                for(int i=0;i<Tokens.length-1;i++){
                    Warehouse warehouse = new Warehouse();
                    warehouse.setOrderNum(orderOV.getOrderNum());
                    warehouse.setStatus("未到达");
                    TransCenter transCenter2 = transCenterService.select_transCenterByLocale(Tokens[i]);
                    int warehouseId = transCenter2.getWarehouseId();
                    warehouse.setWarehouseId(warehouseId);
                    warehouseService.insert_wareHouse(warehouse);
                }
            }
        }
        orderOVService.update_OrderOVByOrderNum(orderOV);
        return CommonResponse.createForSuccess("更新完成");
    }


}
